package com.eenie.common.views

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.text.TextUtils
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.ViewUtils
import androidx.core.view.postDelayed
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import co.zsmb.rainbowcake.base.ViewModelScope
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SizeUtils
import com.eenie.common.R
import com.eenie.common.databinding.ViewAppBarBinding
import com.qmuiteam.qmui.alpha.QMUIAlphaButton
import com.qmuiteam.qmui.alpha.QMUIAlphaImageButton
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import dev.chrisbanes.insetter.applyInsetter

class AppBar @JvmOverloads constructor(var mContext: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : FrameLayout(mContext, attrs, defStyleAttr),
    View.OnClickListener {


    private var mTitle: String = ""
    private var mSubTitle: String = ""
    private var mTargetActivity: Activity? = null

    private lateinit var mBinding: ViewAppBarBinding


    init {
        mBinding = ViewAppBarBinding.inflate(LayoutInflater.from(mContext))
        addView(mBinding.root)
        mBinding.btnBack.setOnClickListener(this)
        if (attrs != null) {
            val array = context.obtainStyledAttributes(attrs, R.styleable.AppBar, 0, 0)
            mTitle = array.getString(R.styleable.AppBar_cus_appbar_title) ?: ""
            val titleSize = array.getDimension(R.styleable.AppBar_cus_appbar_title_size, SizeUtils.sp2px(14f).toFloat())
            val titleColor = array.getColor(R.styleable.AppBar_cus_appbar_title_color, resources.getColor(R.color.qmui_config_color_white))
            mBinding.tvTitle.textSize = SizeUtils.px2sp(titleSize).toFloat()
            mBinding.tvTitle.setTextColor(titleColor)
            setTitle(mTitle)
            mSubTitle = array.getString(R.styleable.AppBar_cus_appbar_subtitle) ?: ""
            val subtitleSize = array.getDimension(R.styleable.AppBar_cus_appbar_subtitle_size, SizeUtils.sp2px(10f).toFloat())
            val subtitleColor = array.getColor(R.styleable.AppBar_cus_appbar_subtitle_color, resources.getColor(R.color.qmui_config_color_white))
            mBinding.tvSubtitle.setTextSize(SizeUtils.px2sp(subtitleSize).toFloat())
            mBinding.tvSubtitle.setTextColor(subtitleColor)
            setSubtitle(mSubTitle)
            var bg = background
            if (bg == null) {
                bg = ColorDrawable(resources.getColor(R.color.colorPrimary))
            }
            background = bg
            val showNavigation = array.getBoolean(R.styleable.AppBar_cus_appbar_show_navigation, true)
            val backResId = array.getResourceId(R.styleable.AppBar_cus_appbar_navigation_icon, R.drawable.qmui_icon_topbar_back)
            mBinding.btnBack.visibility = if (showNavigation) VISIBLE else GONE
            mBinding.btnBack.setImageResource(backResId)
            val showDivider = array.getBoolean(R.styleable.AppBar_cus_appbar_show_divider, false)
            val dividerColor = array.getColor(R.styleable.AppBar_cus_appbar_divider_color, resources.getColor(R.color.qmui_config_color_white))
            val dividerHeight = array.getDimension(R.styleable.AppBar_cus_appbar_divider_height, SizeUtils.dp2px(0.5f).toFloat()).toInt()

            mBinding.viewDivider.visibility = if (showDivider) VISIBLE else GONE
            mBinding.viewDivider.setBackgroundColor(dividerColor)
            mBinding.viewDivider.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dividerHeight)


            if (fitsSystemWindows) {
                mBinding.layContent.applyInsetter {
                    type(statusBars = true) {
                        margin()
                    }
                }
            }
            array.recycle()
        }


        if (mContext is Activity) {
            bind(mContext as Activity)
        } else if (mContext is FragmentActivity) {
            bind(mContext as FragmentActivity)
        }
    }

    fun setTitle(title: String) {
        mTitle = title
        if (TextUtils.isEmpty(title)) {
            mBinding.tvTitle.visibility = GONE
        } else {
            mBinding.tvTitle.visibility = VISIBLE
            mBinding.tvTitle.text = title
        }
    }

    fun setSubtitle(subtitle: String) {
        mSubTitle = subtitle
        if (TextUtils.isEmpty(subtitle)) {
            mBinding.tvSubtitle.visibility = GONE
        } else {
            mBinding.tvSubtitle.visibility = VISIBLE
            mBinding.tvSubtitle.text = subtitle
        }
    }


    fun addTextMenu(id: Int, text: String, listener: OnClickListener?): QMUIAlphaButton {
        val button = createTextMenu(text)
        mBinding.layMenu.addView(button)
        if (listener != null) {
            button.setOnClickListener(listener)
        }
        return button
    }

    fun addImageMenu(id: Int, resId: Int, listener: OnClickListener?): QMUIAlphaImageButton {
        val button = createImageMenu(resId)
        mBinding.layMenu.addView(button)
        if (listener != null) {
            button.setOnClickListener(listener)
        }
        return button
    }


    private fun createTextMenu(text: String): QMUIAlphaButton {
        val menu = QMUIAlphaButton(mContext)
        menu.layoutParams = ViewGroup.LayoutParams(SizeUtils.dp2px(45f), SizeUtils.dp2px(45f))
        menu.text = text
        menu.gravity = Gravity.CENTER
        menu.setBackgroundColor(Color.TRANSPARENT)
        menu.textSize = SizeUtils.px2sp(SizeUtils.sp2px(12f).toFloat()).toFloat()
        menu.setTextColor(Color.WHITE)
        menu.minWidth = 0
        menu.minHeight = 0
        return menu
    }

    private fun createImageMenu(resId: Int): QMUIAlphaImageButton {
        val menu = QMUIAlphaImageButton(mContext)
        menu.layoutParams = ViewGroup.LayoutParams(SizeUtils.dp2px(45f), SizeUtils.dp2px(45f))
        menu.setBackgroundColor(Color.TRANSPARENT)
        menu.setImageResource(resId)
        return menu
    }


    fun bind(activity: Activity?) {
        mTargetActivity = activity
    }

    override fun onClick(v: View?) {
        mTargetActivity?.onBackPressed()
    }


}